package com.sds.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.sds.util.Util_DB;
import com.sds.vo.MemberVO;

public class MemberDAO {
	public void insertMember(MemberVO member) {
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = Util_DB.connection();

			if(conn != null) {				
				pstmt = conn.prepareStatement("insert into t_member values(?, ?, ?, ?, ?)");

				pstmt.setString(1, member.getMem_id());
				pstmt.setString(2, member.getMem_pw());
				pstmt.setString(3, member.getMem_name());
				pstmt.setString(4, member.getMem_tel());
				pstmt.setString(5, member.getMem_per());
				
				pstmt.execute();
			} else {
				System.out.println("Connection Error - [ Location : Member ]");
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(null, pstmt, conn);
		}
	}

	public MemberVO selectMember(String mem_id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		MemberVO member = null;

		try {
			conn = Util_DB.connection();

			if(conn != null) {
				pstmt = conn.prepareStatement("select * from t_member where mem_id = ?");

				pstmt.setString(1, mem_id);

				rs = pstmt.executeQuery();

				while(rs.next()) {
					member = new MemberVO(rs.getString("mem_id"), rs.getString("mem_pw"), rs.getString("mem_name"), rs.getString("mem_tel"), rs.getString("mem_per"));
				}
			} else {
				System.out.println("Connection Error - [ Location : Member ]");
			} 
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(rs, pstmt, conn);
		}

		return member;
	}

	public ArrayList<MemberVO> selectMemberList() {
		Connection conn = null;
		PreparedStatement pstmt = null;		
		ResultSet rs = null;

		ArrayList<MemberVO> memberList = new ArrayList<MemberVO>();

		try {
			conn = Util_DB.connection();

			if(conn != null) {
				pstmt = conn.prepareStatement("select * from t_member");

				rs = pstmt.executeQuery();

				while(rs.next()) {
					memberList.add(new MemberVO(rs.getString("mem_id"), rs.getString("mem_pw"), rs.getString("mem_name"), rs.getString("mem_tel"), rs.getString("mem_per")));
				}
			} else {
				System.out.println("Connection Error - [ Location : Member ]");					
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(rs, pstmt, conn);
		}


		return memberList;
	}

	public void updateMember(MemberVO member) {
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = Util_DB.connection();

			if(conn != null) {
				pstmt = conn.prepareStatement("update t_member set mem_pw = ?, mem_name = ?, mem_tel = ?, mem_per = ? where mem_id = ?");

				pstmt.setString(1, member.getMem_pw());
				pstmt.setString(2, member.getMem_name());
				pstmt.setString(3, member.getMem_tel());
				pstmt.setString(4, member.getMem_per());
				pstmt.setString(5, member.getMem_id());

				pstmt.executeUpdate();
			} else {
				System.out.println("Connection Error - [ Location : Member ]");
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(null, pstmt, conn);
		}

	}

	public void deleteMember(String mem_id) {
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = Util_DB.connection();

			if(conn != null) {
				pstmt = conn.prepareStatement("delete t_member where mem_id = ?");

				pstmt.setString(1, mem_id);

				pstmt.executeUpdate();				
			} else {
				System.out.println("Connection Error - [ Location : Member ]");			
			}	
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(null, pstmt, conn);
		}		
	}
}